<?php

/**
 * 
 * 
 * CREATE  TABLE IF NOT EXISTS `bayes_category` (
  `bayes_category_id` INT NOT NULL AUTO_INCREMENT ,
  `category_text` VARCHAR(80) NULL ,
  `category_feature_count` INT NULL ,
  `category_message_count` INT NULL ,
  PRIMARY KEY (`bayes_category_id`) ,
  UNIQUE INDEX `feature_text_UNIQUE` (`category_text` ASC) )
ENGINE = InnoDB;
 * 
 */
class Bayes_category_data_Model extends BayesDataTemplate
{
	
	// Table primary key and value
	protected $primary_key				= 'bayes_category_id';

	protected $primary_val				= 'category';

	
	
	
	
	public function update_category_feature_count($count, $save = TRUE)
	{
		$this->category_feature_count = $count;
		if($save)
		{
			$this->save();
		}
		
		return $this->category_feature_count;
	}
	
	public function increment_category_message_count($save = TRUE)
	{
		$this->category_message_count = $this->category_message_count? $this->category_message_count + 1 : 1;
		if($save)
		{
			$this->save();
	
		}
		
		return $this->category_message_count;
	}
	
	public function get_total_categories()
	{
		$obj = clone $this;
		$ret = $obj->where(array())->count_all();
		unset($obj);
		return $ret;
	}
	
	/**
	 * Get the total number of messages across all categories:
	 * 
	 * sum(category_message_count) for all categories
	 * ONLY use in train
	 * 
	 * @return type 
	 */
	public function get_total_messages()
	{
		$obj = clone $this->db;
		$obj->from($this->table_name);
		$obj->select('sum(category_message_count) as total');
		$ret = $obj->get();
		return $ret->current()->total;
		
	}
	
	
	
	public function get_all_categories()
	{
		$obj = clone $this->db;
		$obj->from($this->table_name);
		$obj->select('category_text');
		$ret = $obj->get();
		$array = array();
		foreach($ret as $result)
		{
			$array[] = BayesDataTemplate::factory('Bayes_category_data_Model',array($this->table_name, $this->db_name, array('category_text'=>$result->category_text)));
		}
		return $array;
	}
	
	
	
}